package com.brillilab.service.core.system.impl;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.common.alioss.OssFileEnum;
import com.brillilab.dao.mapper.system.FileDownloadMapper;
import com.brillilab.dao.mapper.system.FileManageMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.enums.file.FileDownloadTypeEnum;
import com.brillilab.domain.po.system.FileDownload;
import com.brillilab.domain.po.system.FileManage;
import com.brillilab.service.core.experiments.IExperimentService;
import com.brillilab.service.core.system.IFileDownloadService;
import com.brillilab.service.core.system.IFileManageService;

import cn.hutool.core.collection.CollUtil;

@Service
public class FileDownloadServiceImpl implements IFileDownloadService {

	@Resource
	private FileDownloadMapper fileDownloadMapper;
	@Resource
	private FileManageMapper fileManageMapper;
	@Resource
	private IFileManageService fileManageService;
	@Resource
	private IExperimentService experimentService;

	@Override
	public FileDownload getFileDownload(Long fileId, Long labMemberId) {
		return fileDownloadMapper.selectOne(new LambdaQueryWrapper<FileDownload>().eq(FileDownload::getFileId, fileId)
				.eq(FileDownload::getLabMemberId, labMemberId));
	}

	@Override
	public void save(Long fileId, Integer attachType, Long labId, Long labMemberId, FileDownloadTypeEnum downloadType) {
		FileDownload download = getFileDownload(fileId, labMemberId);
		if (download == null) {
			download = new FileDownload();
			download.setFileId(fileId);
			download.setLabId(labId);
			download.setLabMemberId(labMemberId);
			if (FileDownloadTypeEnum.AIO.getValue().equals(downloadType.getValue())) {
				download.setApp(BoolEnum.TRUE.getValue());
				download.setPc(BoolEnum.TRUE.getValue());
			} else if (FileDownloadTypeEnum.APP.getValue().equals(downloadType.getValue())) {
				download.setApp(BoolEnum.TRUE.getValue());
				if (OssFileEnum.AnalyzeDataFileThumbnail.getKey().equals(attachType)) {
					download.setPc(BoolEnum.TRUE.getValue());
				}
			} else {
				download.setPc(BoolEnum.TRUE.getValue());
				if (OssFileEnum.PrimaryDataFile.getKey().equals(attachType)) {
					download.setApp(BoolEnum.TRUE.getValue());
				}
			}
			fileDownloadMapper.insert(download);
		} else {
			if (FileDownloadTypeEnum.APP.getValue().equals(downloadType.getValue())
					&& BoolEnum.FALSE.getValue().equals(download.getApp())) {
				download.setApp(BoolEnum.TRUE.getValue());
				fileDownloadMapper.updateById(download);
			} else if (FileDownloadTypeEnum.PC.getValue().equals(downloadType.getValue())
					&& BoolEnum.FALSE.getValue().equals(download.getPc())) {
				download.setPc(BoolEnum.TRUE.getValue());
				fileDownloadMapper.updateById(download);
			}
		}
	}

	@Override
	public List<Long> getFileIds(Integer type, Long sourceId) {
		if (type == 1) {
			return this.getTopicFileIds(sourceId);
		}
		return CollUtil.newArrayList();
	}

	private List<Long> getTopicFileIds(Long topicId) {
		List<Long> fileIds = CollUtil.newArrayList();
		List<Integer> fileEnums = CollUtil.newArrayList(OssFileEnum.Topic.getKey(),
				OssFileEnum.TopicProjectPlanFile.getKey(), OssFileEnum.TopicProjectBudgetFile.getKey(),
				OssFileEnum.TopicResultDataFile.getKey(), OssFileEnum.TopicRepresentativeWorksFile.getKey(),
				OssFileEnum.TopicPatentFile.getKey(), OssFileEnum.TopicOtherFile.getKey());
		List<FileManage> topicFileList = fileManageService.selectList(fileEnums, CollUtil.newArrayList(topicId), null);
		if (CollUtil.isNotEmpty(topicFileList)) {
			List<Long> list = topicFileList.stream().map(FileManage::getId).collect(Collectors.toList());
			fileIds.addAll(list);
		}

		List<Long> experimentIds = experimentService.selectIdByTopicIds(CollUtil.newArrayList(topicId));
		if (CollUtil.isNotEmpty(experimentIds)) {
			fileEnums = CollUtil.newArrayList(OssFileEnum.Experiment.getKey(), OssFileEnum.AnalyzeDataFile.getKey(),
					OssFileEnum.PrimaryDataFile.getKey(), OssFileEnum.AuditFile.getKey(),
					OssFileEnum.RemarksDataFile.getKey(), OssFileEnum.AnalyzeDataFileThumbnail.getKey());
			List<FileManage> experimentList = fileManageService.selectList(fileEnums, experimentIds, null);
			if (CollUtil.isNotEmpty(experimentList)) {
				List<Long> list = experimentList.stream().map(FileManage::getId).collect(Collectors.toList());
				fileIds.addAll(list);
			}
		}
		return fileIds;
	}

	@Override
	public List<FileDownload> getFileDownloadList(Long fileId) {
		return fileDownloadMapper.selectList(new LambdaQueryWrapper<FileDownload>().eq(FileDownload::getFileId, fileId)
				.eq(FileDownload::getApp, BoolEnum.TRUE.getValue()).eq(FileDownload::getPc, BoolEnum.TRUE.getValue()));
	}

	@Override
	public List<FileManage> getDownloadFileList(Long userId, String startDate, String endDate) {
		return fileManageMapper.selectList(new LambdaQueryWrapper<FileManage>().eq(FileManage::getUserId, userId)
				.eq(FileManage::getIsDelete, BoolEnum.FALSE.getValue())
//				.eq(FileManage::getIsDownload, BoolEnum.TRUE.getValue())
				.in(FileManage::getAttachType,
						CollUtil.toList(OssFileEnum.AnalyzeDataFile.getKey(), OssFileEnum.PrimaryDataFile.getKey(),
								OssFileEnum.AnalyzeDataFileThumbnail.getKey()))
				.ge(FileManage::getCreateTime, startDate).le(FileManage::getCreateTime, endDate));
	}

}
